package com.hfxt.dao;

import com.hfxt.entity.Users;
import com.hfxt.tool.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.util.List;
import java.util.Map;

public class UsersDaoImpl implements  UsersDao {


    //SqlSession  执行数据库的增删改操作
    public int getCount() {
        Reader reader=null;
        SqlSessionFactoryBuilder builder=null;
        SqlSessionFactory factory=null;
        SqlSession sqlSession=null;
        int result=0;
        //读取配置文件  获得链接数据库的信息
        try{
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            //2、构建sqlsessionFactoryBuiler
            builder=new SqlSessionFactoryBuilder();
            //3、通过SqlSessionFactoryBuilder 构建出SqlSessionFactory工厂类
            factory=builder.build(reader);
            //4、创建sqlSession
            sqlSession=factory.openSession();
            //5、操作数据库  dao接口全限定名+id名
            //result=sqlSession.selectOne("com.hfxt.dao.UsersDao.count");
            result=sqlSession.getMapper(UsersDao.class).getCount();
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return  -1;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<Users> getUsers() {
        Reader reader=null;
        SqlSessionFactoryBuilder builder=null;
        SqlSessionFactory factory=null;
        SqlSession sqlSession=null;
        List<Users>  list=null;
        //读取配置文件  获得链接数据库的信息
        try{
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            //2、构建sqlsessionFactoryBuiler
            builder=new SqlSessionFactoryBuilder();
            //3、通过SqlSessionFactoryBuilder 构建出SqlSessionFactory工厂类
            factory=builder.build(reader);
            //4、创建sqlSession
            sqlSession=factory.openSession();
            //5、操作数据库  dao接口全限定名+id名
            //result=sqlSession.selectOne("com.hfxt.dao.UsersDao.count");
            list=sqlSession.getMapper(UsersDao.class).getUsers();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<Users> getUsersByUsername(String username) {
        Reader reader=null;
        SqlSessionFactoryBuilder builder=null;
        SqlSessionFactory factory=null;
        SqlSession sqlSession=null;
        List<Users>  list=null;
        //读取配置文件  获得链接数据库的信息
        try{
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            //2、构建sqlsessionFactoryBuiler
            builder=new SqlSessionFactoryBuilder();
            //3、通过SqlSessionFactoryBuilder 构建出SqlSessionFactory工厂类
            factory=builder.build(reader);
            //4、创建sqlSession
            sqlSession=factory.openSession();
            //5、操作数据库  dao接口全限定名+id名
            //result=sqlSession.selectOne("com.hfxt.dao.UsersDao.count");
            list=sqlSession.getMapper(UsersDao.class).getUsersByUsername(username);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<Users> getUsersByCondition1(String username, Integer money) {

        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            List<Users> list=sqlSession.getMapper(UsersDao.class).getUsersByCondition1(username,money);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<Users> getUsersByCondition2(Map<String, Object> maps) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            List<Users> list=sqlSession.getMapper(UsersDao.class).getUsersByCondition2(maps);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<Users> getUsersByCondition3(Users users) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            List<Users> list=sqlSession.getMapper(UsersDao.class).getUsersByCondition3(users);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<Users> getUsersByCondition4(Users users) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            List<Users> list=sqlSession.getMapper(UsersDao.class).getUsersByCondition4(users);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<Users> getUsersByCondition5(String choice) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            List<Users> list=sqlSession.getMapper(UsersDao.class).getUsersByCondition5(choice);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<Users> getUsersByCondition6(Integer[] roleids) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            List<Users> list=sqlSession.getMapper(UsersDao.class).getUsersByCondition6(roleids);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<Users> getUsersByCondition7(List<Integer> roleids) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            List<Users> list=sqlSession.getMapper(UsersDao.class).getUsersByCondition7(roleids);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int addUsers(Users users) {
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsersDao.class).addUsers(users);
            sqlSession.commit();
            return  result;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int updateUsers(Users users) {
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsersDao.class).updateUsers(users);
            sqlSession.commit();
            return  result;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    public int getCount2(Users users) {
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UsersDao.class).getCount2(users);
            return  result;
        }catch (Exception e){
            e.printStackTrace();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<Users> getUserByPage(Users users, Integer pageIndex, Integer pageSize) {
        SqlSession sqlSession=null;
        List<Users> list=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            list=sqlSession.getMapper(UsersDao.class).getUserByPage(users,pageIndex,pageSize);
            return  list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public Users getUsersByiD(Integer userid) {
       SqlSession sqlSession= MyBatisUtil.getSession();
        Users user=sqlSession.getMapper(UsersDao.class).getUsersByiD(userid);
        System.out.println(user.getUsername());
        MyBatisUtil.closeSession();
        sqlSession= MyBatisUtil.getSession();
        Users user2=sqlSession.getMapper(UsersDao.class).getUsersByiD(userid);
        System.out.println(user2.getUsername());
        System.out.println(user==user2);
        return user;
    }
}
